home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / dos_win / winsock / maillist / 94-02.Z / 94-02 / text0285.txt < prev    next >
Encoding:
Text File  |  1994-02-28  |  1.4 KB  |  44 lines

  1.  
  2. >     I am trying to find out the dotted IP address of the machine to which a
  3. >socket is connected.  My program listens for connection attempts and 
  4. >acceptsthem, so I don't know the remote machine's address to begin with.  I 
  5. >can call 'getpeername' and have it fill a sockaddr structure, but I don't 
  6. >know where to go from there.  The sockaddr data structure consists of a 
  7. >family field and a 14 byte address array according to the winsock header 
  8. >file.  I have tried calling 'inet_ntoa' with the address array to no avail.
  9. >     Any ideas?
  10.  
  11.  
  12.  
  13.    struct sockaddr sad; 
  14.    struct in_addr sad_tmp;
  15.    struct hostent *reqhostent;
  16.    LPSTR szHostIP;
  17.    
  18.    LPSTR msg_buf[100];
  19.    int len = sizeof(sad);
  20.    
  21.    
  22.    /* take connection request off queue, and disable further
  23.       notifications.  we can process only one request at a time. */
  24.  
  25.    ConnectSocket = accept(ListenSocket, &sad, &len);
  26.    WSAAsyncSelect(ListenSocket, hFrame, 0, 0);
  27.    
  28.    sad_tmp.S_un.S_un_b.s_b1 = sad.sa_data[2];
  29.    sad_tmp.S_un.S_un_b.s_b2 = sad.sa_data[3];
  30.    sad_tmp.S_un.S_un_b.s_b3 = sad.sa_data[4];
  31.    sad_tmp.S_un.S_un_b.s_b4 = sad.sa_data[5];
  32.    
  33.    szHostIP = inet_ntoa( sad_tmp );
  34.    reqhostent = gethostbyaddr( &sad_tmp, 4, PF_INET );
  35.    if( NULL == reqhostent ){
  36.        wsprintf( InfoBuf, (LPSTR) "Query from host %s, could not resolve 
  37. the name.\n", szHostIP );
  38.    } else {  
  39.           wsprintf( InfoBuf, (LPSTR) "Query from host %s, %s \n", szHostIP, 
  40. reqhostent->h_name );
  41.    }   
  42.  
  43.  
  44.